package com.simtop.picture.service.imp;

import com.simtop.picture.common.ServerResponse;
import com.simtop.picture.mapper.UserMapper;
import com.simtop.picture.pojo.Role;
import com.simtop.picture.pojo.User;
import com.simtop.picture.service.UserService;
import com.simtop.picture.utils.AliyunMailUtil;
import com.simtop.picture.utils.SmsRandomCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Autowired
    public UserMapper userMapper;

    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    @Override
    public List<Role> getRole() {
        return userMapper.getRole();
    }

    @Override
    public ServerResponse<String> register(User user) {
        String checkCode = redisTemplate.boundValueOps("email_"+user.getEmail()).get();
        if(StringUtils.isEmpty(checkCode)){
            return ServerResponse.createByErrorMsg("验证码已失效");
        }
        else if(!user.getCheckCode().equals(checkCode)){
            return ServerResponse.createByErrorMsg("验证码错误");
        }else{
            user.setCreateTime(new Date());
            User checkEmail=userMapper.checkEmail(user.getEmail());
            if(ObjectUtils.isEmpty(checkEmail)){
                int i=userMapper.register(user);
                if(i!=0){
                    return ServerResponse.createBySuccessMsg("注册成功");
                }else{
                    return ServerResponse.createByErrorMsg("注册失败");
                }
            }else{
                return ServerResponse.createByErrorMsg("该邮箱已经注册过了!");
            }

        }
    }



    /**
     * 邮箱验证
     * @param email
     * @return
     */
    @Override
    public ServerResponse<String> generateCheckCode(String email) {
        synchronized (this){
            //邮箱非空验证
            if(email==null||"".equals(email)){
                return ServerResponse.createByErrorMsg("邮箱不能为空,请填入有效地址");
            }
            //生成随机的6位数字验证码
            String verificationCode= SmsRandomCodeUtil.generateRandomSixNum();
            //调用邮箱发送
            AliyunMailUtil.sendMail(email,"家纺项目","您的验证码为："+verificationCode+"。此验证码三分钟内有效，请及时注册。"+"<br><br>"+"本邮件是系统自动发送的，请勿直接回复！感谢您的注册，祝您使用愉快！");
            redisTemplate.boundValueOps("email_"+email).set(verificationCode,180, TimeUnit.SECONDS);
            // todo 发送的验证码拼接了之前的验证码？？ 获取content内容时候出现getContent现象
            // todo 2019年8月28日09:23:18 接口中屏蔽验证码
            return ServerResponse.createBySuccessMsg("发送验证码成功");
        }
    }

    /**
     * 忘记密码时候，根据邮箱返回验证码
     * @param email
     * @return
     */
    @Override
    public ServerResponse<String> forgetSendEmailCode(String email) {
        //验证邮箱是否存在
        User resultCount = userMapper.checkEmail(email);
        if(ObjectUtils.isEmpty(resultCount)){
            return ServerResponse.createByErrorMsg("邮箱不存在！");
        }
        //生成随机的验证码
        String verificationCode= SmsRandomCodeUtil.generateRandomSixNum();
        //调用邮箱发送
        AliyunMailUtil.sendMail(email,"家纺项目","您的验证码为："+verificationCode+"。此验证码五分钟内有效，请及时重置密码。"+"<br><br>"+"本邮件是系统自动发送的，请勿直接回复！");
        //发送成功保存邮箱地址对应的验证码 todo 3分钟 已解决
        redisTemplate.boundValueOps("forget_"+email).set(verificationCode,300,TimeUnit.SECONDS);
        return ServerResponse.createBySuccessMsg("验证码已经发送");
    }

    /**
     * 修改密码
     * @param user
     * @return
     */
    @Override
    public ServerResponse<String> updatePassword(User user) {
        String checkCode = (String) redisTemplate.boundValueOps("forget_" + user.getEmail()).get();
        int updatePasswordResult=0;
        if (StringUtils.isEmpty(checkCode)) {
            return ServerResponse.createByErrorMsg("验证码已经失效");
        } else {
            if (!checkCode.equals(user.getCheckCode())) {
                return ServerResponse.createByErrorMsg("验证码输入错误");
            } else {
                User checkEmailUser = userMapper.checkEmail(user.getEmail());
                if (!ObjectUtils.isEmpty(checkEmailUser)) {
                     updatePasswordResult = userMapper.updatePassword(user);
                }
            }
        }
        if (updatePasswordResult != 0) {
            return ServerResponse.createBySuccessMsg("修改成功");
        } else {
            return ServerResponse.createByErrorMsg("修改失败");
        }
    }
}
